package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITemplateFieldDAO;
import org.jsola.hr.dto.TemplateFieldAddDTO;
import org.jsola.hr.dto.TemplateFieldUpdateDTO;
import org.jsola.hr.entity.TemplateFieldDO;
import org.jsola.hr.query.TemplateFieldQuery;
import org.jsola.hr.service.ITemplateFieldService;
import org.jsola.hr.vo.TemplateFieldListVO;
import org.jsola.hr.vo.TemplateFieldVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrTemplateFieldServiceImpl")
public class TemplateFieldServiceImpl implements ITemplateFieldService {

    @Autowired
    private ITemplateFieldDAO templateFieldDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TemplateFieldVO save(TemplateFieldAddDTO templateFieldAddDTO, TokenUser tokenUser) {
        // 转DO
        TemplateFieldDO templateFieldDO = templateFieldAddDTO.to(TemplateFieldDO.class);
        // 保存
        templateFieldDO = save(templateFieldDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return templateFieldDO.to(TemplateFieldVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TemplateFieldUpdateDTO templateFieldUpdateDTO, TokenUser tokenUser) {
        //转DO
        TemplateFieldDO templateFieldDO = templateFieldUpdateDTO.to(TemplateFieldDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(templateFieldDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... templateFieldIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) templateFieldIds);
    }


    @Override
    public TemplateFieldVO selectById(String templateFieldId, String siteId) {
        TemplateFieldDO templateFieldDO = selectDOById(templateFieldId, siteId);
        if (templateFieldDO == null) {
            return null;
        }
        return templateFieldDO.to(TemplateFieldVO.class);
    }

    @Override
    public List<TemplateFieldListVO> select(TemplateFieldQuery templateFieldQuery, String siteId) {
        List<TemplateFieldDO> templateFieldDOList = selectDO(templateFieldQuery, siteId);
        if(CollectionUtils.isEmpty(templateFieldDOList)) {
            return templateFieldDOList == null ? null : new ArrayList<>();
        }
        return templateFieldDOList.stream()
                .map(templateFieldDO -> templateFieldDO.to(TemplateFieldListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TemplateFieldQuery templateFieldQuery, String siteId) {
        Example example = buildExample(templateFieldQuery,siteId);
        return templateFieldDAO.selectCountByExample(example);
    }

    @Override
    public Page<TemplateFieldListVO> selectPage(TemplateFieldQuery templateFieldQuery, String siteId) {
        Example example = buildExample(templateFieldQuery,siteId);
        Page<TemplateFieldDO> page = templateFieldDAO.selectPageByExample(example,
                templateFieldQuery.getPageNo(),
                templateFieldQuery.getPageSize());

        return page.to(TemplateFieldListVO.class);
    }

    @Override
    public TemplateFieldDO selectDOById(String templateFieldId, String siteId) {
        return listById(templateFieldId, siteId);
    }

    @Override
    public List<TemplateFieldDO> selectDO(TemplateFieldQuery templateFieldQuery, String siteId) {
        Example example = buildExample(templateFieldQuery,siteId);
        return templateFieldDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param templateFieldQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TemplateFieldQuery templateFieldQuery, String siteId) {
        Example example = new Example(TemplateFieldDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (templateFieldQuery.getTemplateId() != null) {
            example.and().andLike("templateId", "%" + templateFieldQuery.getTemplateId() + "%");
        }
        if (templateFieldQuery.getField() != null) {
            example.and().andLike("field", "%" + templateFieldQuery.getField() + "%");
        }
        if (templateFieldQuery.getComment() != null) {
            example.and().andLike("comment", "%" + templateFieldQuery.getComment() + "%");
        }
        if (templateFieldQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", templateFieldQuery.getCompanyId());
        }
        if (templateFieldQuery.getGmtCreateStart() != null &&
                templateFieldQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", templateFieldQuery.getGmtCreateStart(),
                    templateFieldQuery.getGmtCreateEnd());
        }
        if (templateFieldQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + templateFieldQuery.getCreateUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,templateFieldQuery.getOrders());
        return example;
    }

    @Override
    public List<TemplateFieldListVO> selectFieldListAll(TemplateFieldQuery templateFieldQuery, String siteId) {
        List<TemplateFieldDO> templateFieldDOList = templateFieldDAO.selectFieldListAll(siteId);
        return templateFieldDOList.stream()
                .map(templateFieldDO -> templateFieldDO.to(TemplateFieldListVO.class))
                .collect(Collectors.toList());
    }
}




